package com.android.djlmapp.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.android.djlmapp.activity.home.HomeActivity;
import com.android.djlmapp.activity.login.LoginActivity;
import com.android.djlmapp.activity.msg.MsgDetailActivity;
import com.android.djlmapp.activity.msg.SystemMsgListActivity;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.tools.global.ConstantsValue;
import com.android.djlmapp.tools.global.PrefUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.android.djlmapp.tools.msg.ExampleUtil;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

/**
 * @name djlm
 * @class name：com.android.djlmapp.receiver
 * @class describe: 消息广播
 * @anthor wangziheng
 * @time 2017/4/26 10:46
 * @change
 * @chang time
 * @class describe
 */
public class MsgReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    private String MsgType;

    private String f_address;
    private String mobile;
    private String agent;
    private String userName;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        MsgType = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String regIid = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
        Log.i(TAG, "onReceive: 推送id " + regIid);
//        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.i(TAG, "onReceive: " + regId);
            if (!TextUtils.isEmpty(regId)) MyApplication.getInstance().setRegistrationId(regId);
            Log.i(TAG, "getPhoneInfo: " + regId);

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.i(TAG, "[MyReceiver] 接收到推送下来>>>>>>title  : " + bundle.getString(JPushInterface.EXTRA_TITLE));
            processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "[MyReceiver] 接收到推送下来的通知>>>>>>title  : " + bundle.getString(JPushInterface.EXTRA_TITLE));
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

            Log.i(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.i(TAG, "[MyReceiver] 用户点击打开了通知");
            //打开自定义的Activity
            Log.i(TAG, "[MyReceiver] 接收到推送下来的通知>>>>>>MsgType  : " + MsgType);
            try {
                JSONObject jsonObject = new JSONObject(MsgType);
                String msgType = jsonObject.optString("type");
                if ("1".equals(msgType)) {
                    f_address = PrefUtils.getString(ConstantsValue.F_ADDRESS, "");
                    mobile = PrefUtils.getString(ConstantsValue.MOBILE, "");
                    agent = PrefUtils.getString(ConstantsValue.AGENT, "");
                    userName = PrefUtils.getString(ConstantsValue.USER_NAME, "");

                    HashMap<String, String> clientInfo = new HashMap<>();
                    clientInfo.put("name", userName);
                    clientInfo.put("用户类型", "店铺");
                    clientInfo.put("tel", mobile);
                    clientInfo.put("address", f_address);
                    clientInfo.put("上级代理商", agent);

                    Intent inte = new MQIntentBuilder(context)
                            .setClientInfo(clientInfo)
                            .setCustomizedId(MyApplication.getInstance().getRegistrationId())
                            .build();
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(inte);
                } else {
                    Intent i = new Intent(context, MsgDetailActivity.class);
                    i.putExtras(bundle);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.i(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//            在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.i(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.i(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    //send msg to ReadCardActivity
    private void processCustomMessage(Context context, Bundle bundle) {
        if (SystemMsgListActivity.isForeground) {
            Log.i(TAG, "processCustomMessage: 进入");
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Intent msgIntent = new Intent(SystemMsgListActivity.MESSAGE_RECEIVED_ACTION);
            msgIntent.putExtra(SystemMsgListActivity.KEY_MESSAGE, message);
            if (!ExampleUtil.isEmpty(extras)) {
                try {
                    JSONObject extraJson = new JSONObject(extras);
                    if (extraJson.length() > 0) {
                        msgIntent.putExtra(SystemMsgListActivity.KEY_EXTRAS, extras);
                    }
                } catch (JSONException e) {
                }
            }
            context.sendBroadcast(msgIntent);
        } else {
            Log.i(TAG, "processCustomMessage: 未通知");
        }
    }
}
